const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
const tools = require("../../tools/tool");
// 领单收款 中手工查询单据，选中单据后再调用findSaleBill接口取数据
router.post('/ldsk/getSaleBillByNo', async ctx => {
    let tenantId = ctx.user.tenantId;
    let billNo = ctx.request.body.billNo;
    let sql = `select a.id,a.billNo,a.transType,a.transTypeName,a.billDate,a.billState,b.name salesName,a.salesId,a.buId,c.name buName 
                from invoice a inner join contact c on a.tenantId=${tenantId} and 
                a.buId=c.id and a.billType='SALE' and  a.billNo like '%${billNo}%' 
               left join admin b on a.salesId=b.id 
    `
    let invo = await db.query(sql, {type: QueryTypes.SELECT})
    let list = []
    for (let i = 0; i < invo.length; i++) {
        let state = ''
        if(invo[i].billState === 'draft') state = '草稿'
        if(invo[i].billState === 'accounted') state = '已审核'
        if(invo[i].billState === 'del') state = '已删除'
        let row = {
            id: invo[i].id,
            billNo: invo[i].billNo,
            transTypeName: invo[i].transTypeName,
            transType: invo[i].transType,
            billDate: invo[i].billDate,
            billState: state,
            salesId: invo[i].salesId,
            salesName: invo[i].salesName,
            buId: invo[i].buId,
            buName: invo[i].buName,
        }
        list.push(row)
    }
    return ctx.suc('', list)
})







module.exports = router;